<?php

// +----------------------------------------------------------------------
// | MyCentOS
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.mycentos.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 琥珀 <317559272@qqq.com><http://www.mycentos.com>
// +----------------------------------------------------------------------
// | @date  : 2015-03-05 14:13:54
// +----------------------------------------------------------------------
// | @Last Modified by:  琥珀
// +----------------------------------------------------------------------
// | @Last Modified time: 2015-03-14 16:34:20
// +-----------------------------------------------------------------------

/**
 * 菜单控制器
 * @authors 琥珀 (317559272@qq.com)
 */
namespace Admin\Controller;
use Think\Controller;
Class MenuController extends AdminController
{
    public function index() {
        $model = M('Menu');
        $searchname = I('searchname');
        if ($searchname) {
            $map['name|url|rel|target'] = array(array('like', '%' . $searchname . '%'), array('like', '%' . $searchname . '%'),array('like', '%' . $searchname . '%'),array('like', '%' . $searchname . '%'), '_multi' => true);
        }
        $pid = I('get.pid', 0);
        $map['pid'] = $pid;
        $order = 'sort';
        $list = $this->lists($model, $map,$order);
        $dialogid = 'dialog_'.$pid;//生成打开dialog的id传输给add模板
        $all_menu = M('Menu')->getField('id,name');
        if ($list) {
            foreach ($list as & $key) {
                if ($key['pid']) {
                    $key['p_name'] = $all_menu[$key['pid']];
                }
            }
            $this->assign('list', $list);
        }
        $this->assign('list', $list);
        $this->assign('searchname', $searchname);
        $this->assign('dialogid', $dialogid);
        $this->display();
    }
    public function add() {
        if (IS_POST) {
            $menu = M('Menu');
            $data = I('post.');
            if (false === $menu->create($data)) {
                $this->error($menu->getError());
            }
            $rs = $menu->add();
            if ($rs !== false) {
                $this->success('新增成功!');
            } else {
                $this->error('新增失败!');
            }
        } else {
            $dialogid = I('dialogid');
            $type = I('type');
            $menu = I('menu');
            $data = M('Menu')->field('id,pid,name,url,rel')->select();
            $this->assign('dialogid', $dialogid);
            $this->assign('type', $type);
            $this->assign('menu', $menu);
            $this->assign('tree', $data);
            $this->display();
        }
    }
    public function edit() {
        if (IS_POST) {
            $Menu = M('Menu');
            $data = I('post.');
            if ($data['id']===$data['pid']) {
               $this->error('不能将自己移动到自己的分类');
            }
            $Menu->create($data);
            $result=$Menu->save();
            if ($result) {
                $this->success('修改成功');
            } else {
                $this->error('修改失败');
            }

        } else {
            $id = I('id');
            $dialogid = I('dialogid');
            if (empty($id)) {
                $this->error('请选择要操作的数据!');
            }
            $map = array('id' => $id);
            $Menu = M("Menu");

            $data = $Menu->where($map)->find();
            $p_name = $Menu->where(array('id'=>$data['pid']))->getField('name');
            $tree = M('Menu')->select();
            $this->assign('dialogid', $dialogid);
            $this->assign('tree', $tree);
            $this->assign('data', $data);
            $this->assign('pid', $data['pid']);
            $this->assign('p_name', $p_name);
            $this->display();
        }
    }
    public function del($ids = null) {
        empty($ids) && $this->error('参数错误');
        $ids = is_array($ids) ? implode(',', $ids) : $ids;
        $map['id'] = array('in', $ids);
        $res = M('Menu')->where($map)->delete();
        if ($res !== false) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}
?>